﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Collections;
using System.Transactions;

#region Desnivel
namespace Distrackting.DataLayer
{
    public partial class Desnivel
    {
        public void setDesnivel(Desnivel d)
        {
            this.cod_desnivel = d.cod_desnivel;
            this.descricao = d.descricao;
        }
    }
}
#endregion


#region dataDesnivel
namespace Distrackting.DataLayer
{
    public class dataDesnivel
    {
        #region Methods
        public static void createDesnivel(Desnivel d)
        {

            try
            {
                dbDataDataContext dbData = new dbDataDataContext();
                d.cod_desnivel = System.Guid.NewGuid();
                dbData.Desnivels.InsertOnSubmit(d);

                dbData.SubmitChanges();

            }
            catch (Exception e)
            {
                throw e;
            }
        }
            

       

        public static Desnivel getDesnivel(Guid guid)
        {
            try
            {
                if (guid == null) return null;
                dbDataDataContext dbData = new dbDataDataContext();

                Desnivel dnv = (from d in dbData.Desnivels
                             where d.cod_desnivel == guid
                             select d).FirstOrDefault();
                return dnv;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static Boolean changeDesnivel(Desnivel d)
        {
            try
            {
                if (d == null)
                    return false;
                else
                {
                    dbDataDataContext dbData = new dbDataDataContext();
                    Desnivel res = dbData.Desnivels.FirstOrDefault(dnv => dnv.cod_desnivel == d.cod_desnivel);
                    if (res == null) return false;
                    else
                    {
                        res.setDesnivel(d);
                        dbData.SubmitChanges();
                        return true;
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static IQueryable<Desnivel> getDesnivels()
        {
            try
            {
                dbDataDataContext dbData = new dbDataDataContext();
                return dbData.Desnivels;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static IQueryable<Desnivel> getDesnivels(Expression<Func<Desnivel, bool>> whereClause)
        {
            try
            {
                dbDataDataContext dbData = new dbDataDataContext();
                return dbData.Desnivels.Where(whereClause);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static Boolean deleteDesnivel(Desnivel d)
        {
            try
            {
                dbDataDataContext dbData = new dbDataDataContext();
                if (dbData.Desnivels.Contains(d))
                {
                    Desnivel res = (from dnv in dbData.Desnivels
                                 where dnv.cod_desnivel == d.cod_desnivel
                                 select dnv).FirstOrDefault();
                    if (res == null)
                        return false;
                    else
                    {
                        dbData.Desnivels.DeleteOnSubmit(d);
                        dbData.SubmitChanges();
                        return true;
                    }
                }
                else return false;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        #endregion
    }
}
#endregion